package com.xianyun.site.support.executor.handle.sms;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.xianyun.common.core.filter.FilterChain;
import com.xianyun.site.support.common.FilterChainFactory;
import com.xianyun.site.support.common.exception.SmsException;
import com.xianyun.site.support.common.utils.SpringBeanUtil;
import com.xianyun.site.support.convertor.SmsConvertor;
import com.xianyun.site.support.domain.model.SmsMessage;
import com.xianyun.site.support.dto.SmsReceiveCmd;
import com.xianyun.site.support.executor.filter.sms.SmsOneMinNotRepeatFilter;
import com.xianyun.site.support.executor.filter.sms.SmsSendTooMuchFilter;
import com.xianyun.site.support.executor.handle.sms.v20201114.config.SmsSendStrategyType;
import org.springframework.util.Assert;

/**
 * @author mac_zyj
 */
public class SmsStrategyContent {

    /**
     * 责任链配置
     */
    private final FilterChain<SmsReceiveCmd> filterChain= FilterChainFactory.buildFilterChain(
            SmsOneMinNotRepeatFilter.class,
            SmsSendTooMuchFilter.class
    );

    /**当前策略*/
    private BaseSmsStrategy currentStrategy;

    public SmsStrategyContent(String type) {
        Assert.notNull(type,"请确认短信类型是否正确");
        if (null != SmsSendStrategyType.getSmsStrategyByType(type)){
            currentStrategy= SpringBeanUtil.getBean(SmsSendStrategyType.getSmsStrategyByType(type));
        }
    }

    /**
     * 初始化短信发送请求
     * @param smsRequest 短信请求
     * @return 发送请求
     */
    public SendSmsRequest initSendSmsRequest(SmsReceiveCmd smsRequest){
        filterChain.doFilter(smsRequest);
        if (null != currentStrategy){
            SmsMessage smsMessage = SmsConvertor.toEntity(smsRequest.getSmsMessage());
            return currentStrategy.initSendSmsRequest(smsMessage);
        }
        throw new SmsException("请确认短信类型是否正确");
    }
}
